package widget

import (
	"clientDemo/service/widget_data"
	"clientDemo/ui/fyne_ui/common"
	"clientDemo/ui/fyne_ui/widget/interface"
	"clientDemo/ui/fyne_ui/widget/utils"
	utils2 "clientDemo/utils"
	"fyne.io/fyne/v2"
	"github.com/ian-kent/go-log/log"
	"os"
)

type SceneXmlParser struct {
	RootNode widgetcommon.IWidgetItem
	path     string

	AddNodes []widgetcommon.IWidgetItem
	//skinList    []*fyne.CanvasObject
	//WidgetInfos []*common.WidgetInfo

	Title      string
	MainWidth  int
	MainHeight int
	Property   []*widget_data.Property
	LoadSkin   widget_data.LoadSkin
}

func NewSceneXmlParser(path string, container widgetcommon.IWidgetItem) *SceneXmlParser {
	o := &SceneXmlParser{
		RootNode: container,
		path:     path,
	}

	data, err := os.ReadFile(path)
	if nil != err || len(data) == 0 {
		return nil
	}
	o.AddNodes = append(o.AddNodes, container)
	loader := widget_data.NewXmlLoader(o)
	loader.Load(data)
	return o
}
func (p *SceneXmlParser) UpdateProperties(prop []*widget_data.Property) {
	for _, v := range prop {
		p.Property = append(p.Property, v)
		switch v.Key {
		case "Title":
			p.Title = v.Value
		case "MainWidth":
			p.MainWidth = utils2.ParseInt(v.Value)
		case "MainHeight":
			p.MainHeight = utils2.ParseInt(v.Value)
		}
	}
}
func (p *SceneXmlParser) UpdateLoadSkin(skinInfo *widget_data.LoadSkin) {
	if skinInfo == nil {
		return
	}
	p.LoadSkin = *skinInfo
}
func (p *SceneXmlParser) StartElement(ctx interface{}, name string, atts interface{}) {
	if name == "root" {
		return
	}
	if len(p.AddNodes) == 0 {
		return
	}
	data, ok := atts.(*widget_data.WidgetData)
	if ok {

	}
	switch name {
	case "LoadSkin":
		// TODO: 处理LoadSkin
	case "Widget":
		// 最后一个元素
		rootWidget := p.AddNodes[len(p.AddNodes)-1]
		if rootWidget == nil {
			return
		}
		/* 重构		emptyWidget := CreatWidgetContainerByName(Default_WidgetContainer_Name, fyne.NewSize(0, 0))
		newNode := p.CreatWidgetBySkin(data.SkinTempName, data.Name, emptyWidget)
		if newNode == nil {
			return
		}

		rootWidget.AddWidgetContainer(emptyWidget, fyne.NewPos(0, 0))
		emptyWidget.AddWidgetChildObj(newNode)
		emptyWidget.SetName(data.Name)
		emptyWidget.SetParent(rootWidget)
		p.updateProperty(emptyWidget, newNode, data.Properties)
		p.AddNodes = append(p.AddNodes, emptyWidget)
		*/
		newNode := p.CreatWidgetBySkin(data.Name, data.SkinTempName, rootWidget)
		if newNode == nil {
			return
		}
		rootWidget.AddWidgetChildObj(newNode)
		UpdateProperty(rootWidget, newNode, data.Properties, true /*widget_data.CastPropertyToMap(data.Properties)*/)
		p.AddNodes = append(p.AddNodes, newNode.(widgetcommon.IWidgetItem))
	}
}
func (p *SceneXmlParser) EndElement(ctx interface{}, name string, atts interface{}) {
	if len(p.AddNodes) == 0 {
		return
	}
	// 去掉最后一个
	switch name {
	case "Widget":
		p.AddNodes = p.AddNodes[:len(p.AddNodes)-1]
	}
}
func (p *SceneXmlParser) updateAttribute(containerWidget widgetcommon.IWidgetItem, node widgetcommon.IWidgetItem, attributes []*widget_data.Property) {
	if len(attributes) == 0 {
		return
	}
	UpdateProperty(containerWidget, node, attributes, false)
}
func UpdateProperty(containerWidget widgetcommon.IWidgetItem, node widgetcommon.IWidgetItem, properties []*widget_data.Property, isProperty bool) {
	if len(properties) == 0 {
		return
	}
	minSize := node.MinSize()
	_ = minSize
	//containerWidget.SetMinSize(minSize) //这个是设置边框值
	//fmt.Printf("SkinName:%v---------------------Size:%v--minSize:%v------------------\n", containerWidget.GetName(), containerWidget.GetBaseSize(), minSize)
	//containerWidget.SetPosition(node.(fyne.CanvasObject), fyne.NewPos(0, 0))
	for i := 0; i < len(properties); i++ {
		v := properties[i]
		if v == nil {
			continue
		}
		key := v.Key
		value := v.Value
		//}
		//for key, value := range properties {
		if key == "Pos" {
			//containerWidget.GetParent().SetPosition(containerWidget.(fyne.CanvasObject), utils.ParsePosition(property.Value))
			containerWidget.SetPosition(node.(fyne.CanvasObject), utils.ParsePosition(value))
			if isProperty {
				node.SetProperty(key, value, isProperty)
			} else {
				node.SetAttribute(key, value)
			}
		}
		if key == "Size" {
			containerWidget.SetMinSize(utils.ParseSize(value))
			if isProperty {
				node.SetProperty(key, value, isProperty)
			} else {
				node.SetAttribute(key, value)
			}
		}
		if key == "ImagicTexture" ||
			key == "NormalTexture" ||
			key == "PressTexture" || //这个支持auto"PressTexture"
			key == "DisableTexture" ||
			key == "BackTexture" ||
			key == "Texture" {
			// 判断value 路径文件是否存在
			if value != "" && value != "ActionPress" && value != "AutoPress" {
				ok, err := utils2.CheckFile(value)
				if !ok {
					log.Error("文件找不到 %v", err)
					continue
				}
			}
		}
		if key == "ImagicTexture" ||
			key == "NormalTexture" ||
			key == "PressTexture" ||
			key == "HoverTexture" ||
			key == "BackTexture" ||
			key == "Texture" ||
			key == "ShowTxt" ||
			key == "FontSize" ||
			key == "Color" ||
			key == "Scale" ||
			key == "Width" ||
			key == "Height" ||
			key == "Visible" ||
			key == "InsetsRect" ||
			key == "FillColor" ||
			key == "BorderColor" ||
			key == "BorderWidth" ||
			key == "AnchorPos" {
			if isProperty {
				node.SetProperty(key, value, isProperty)
			} else {
				node.SetAttribute(key, value)
			}
		}
	}
	node.(fyne.CanvasObject).Refresh()
	containerWidget.Refresh()
}

func (p *SceneXmlParser) CreatWidgetBySkin(name string, skinName string, rootNode widgetcommon.IWidgetItem) widgetcommon.IWidgetItem {
	widgetInfo := common.Get().GetSkin(skinName)
	if widgetInfo == nil {
		return nil
	}
	newNode := CreatWidgetByName(name, widgetInfo.GetBaseWidgetName(), widgetInfo, rootNode)
	if newNode == nil {
		return nil
	}
	widgetInfo.ForEachChild(func(skinInfo widgetcommon.ISkinInfo) {
		node2 := p.CreatWidgetBySkin("", skinInfo.GetSkinName(), newNode.(widgetcommon.IWidgetItem))
		if node2 == nil {
			return
		}
	})
	p.updateAttribute(rootNode, newNode, widgetInfo.GetAttribute())
	p.updateAttribute(rootNode, newNode, widgetInfo.GetAllProperty())
	return newNode
}
